// Copyright 2023 Srhino Co., Ltd. All rights reserved.

package log

import (
	"fmt"
	"time"

	"go.uber.org/zap/zapcore"
)

// Copied from https://github.com/uber-go/zap/blob/v1.24.0/zapcore/encoder.go
func encodeTimeLayout(t time.Time, layout string, enc zapcore.PrimitiveArrayEncoder) {
	type appendTimeEncoder interface {
		AppendTimeLayout(time.Time, string)
	}

	if enc, ok := enc.(appendTimeEncoder); ok {
		enc.AppendTimeLayout(t, layout)
		return
	}

	enc.AppendString(t.Format(layout))
}

// TimeEncoder is a function with specified parameters.
func ISO8601TimeEncoderForShanghaiLocalization(t time.Time, enc zapcore.PrimitiveArrayEncoder) {
	location, err := time.LoadLocation("Asia/Shanghai")
	if err != nil {
		fmt.Println(err)
	}
	if location != nil {
		t = t.In(location)
	}
	encodeTimeLayout(t, "2006-01-02T15:04:05.000Z0700", enc)
}
